﻿using System.Windows;

namespace XamlMvp
{
    public class Presenter<TView> where TView : IView
    {
        readonly TView view;
        static readonly AutoEventBinder autoEventBinder;

        public TView View { get { return view; } }

        static Presenter()
        {
            autoEventBinder = new AutoEventBinder(typeof(TView));
        }

        public Presenter(TView view)
        {
            this.view = view;
            
            autoEventBinder.BindEvents(View, this);

            View.Unloaded += Unloaded;
        }

        void Unloaded(object sender, RoutedEventArgs e)
        {
            ReleaseView();
            
            autoEventBinder.UnBindEvents(View, this);

            View.Unloaded -= Unloaded;
        }

        protected virtual void ReleaseView() { }
    }
}
